﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using MoskieBlog.Data.Models;
using MoskieBlog.Data;

namespace MoskieBlog.Controllers.Api
{
    public class PostController : ApiController
    {
        private MoskieBlogDbContext db = new MoskieBlogDbContext();

        // GET /api/post
        public IEnumerable<Post> Get()
        {
            return db.Posts;
        }

        // GET /api/post/5
        public IEnumerable<Post> Get(int id)
        {
            return db.Posts.Where(p => p.PostId == id);
        }

        // POST /api/post
        public void Post(Post value)
        {
            db.Posts.Add(value);

            db.SaveChanges();
        }

        // PUT /api/post/5
        public void Put(int id, Post value)
        {
            Post post = db.Posts.Single(p => p.PostId == id);
            
            post.Title = value.Title;
            post.Text = value.Text;

            db.SaveChanges();
        }

        // DELETE /api/post/5
        public void Delete(int id)
        {
            db.Posts.Remove(db.Posts.Single(p => p.PostId == id));

            db.SaveChanges();
        }
    }
}
